<?php

class divisions
{
	var $mess;
	var $champid;
	var $toRender = Array();

	function divisions($champid)
	{
		$mess = '';
		$this->champid = $champid;
		if(isset($_POST['removeRecord']) && isset($_POST['id']))
		{
			$this->doDelete();
		}
		else if(isset($_POST['addRecord']) && isset($_POST['id']))
		{
			$this->doAdd();
		}
		else if(isset($_POST['editRecord']) && isset($_POST['id']))
		{
			$this->doEdit();
		}
		$this->setupEdit();
		$this->setupAdd();
	}

	function render()
	{		
		include_once('includes/addMessage.php');
		
		if($this->mess != '')
		{
			$this->mess->render();
		}
		
		foreach( $this->toRender as $key => $value){
			$value->render();
		}
		
	}
	
	function doDelete()
	{
		global $skin_dir, $db, $lang;
		
		//get the record
		$qSel = "select d_title, d_champid from ". DIVISIONS_TABLE . " Where d_id = " . $_POST['id'];
		$result = $db->sql_query($qSel);
		if($row = $db->sql_fetchrow($result)) 
		{
			$qUpdateDivisions = 'UPDATE ' . CHAMPIONSHIPS_TABLE . ' SET c_divisions=(SELECT COUNT(*) FROM ' . DIVISIONS_TABLE . 
						' WHERE d_champid = ' .  $this->champid . ') WHERE c_id = ' . $this->champid;
			$qDel = "delete from ". DIVISIONS_TABLE . " Where d_id = " . $_POST['id'];
			
			$result = $db->sql_query($qDel);
			$db->sql_query($qUpdateDivisions);
			$this->mess = new addMessage(str_replace('{entry}', $row['d_title'], $lang['AdminRemoved']), $lang['AdminRemovedTitle']);
			
		}
		else
		{
			$this->mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
			
		}
	}
	
	function doAdd()
	{
		global $db, $lang;
		
		if($this->validateFields(false)) 
		{
			$qUpd = "insert into  ". DIVISIONS_TABLE . " (d_title, d_order, d_champid, d_signuplimit, d_drivermaxevents, d_ranking) values('" .
							 $_POST['title'] . "', " . $_POST['order'] .", " . $this->champid . ", " . $_POST['signuplimit'] . ',' . 
							 $_POST['eventslimit'].', ' . $_POST['ranking']. ')';
			$result = $db->sql_query($qUpd);
                        echo $qUpd;
			$qUpdateDivisions = 'UPDATE ' . CHAMPIONSHIPS_TABLE . ' SET c_divisions=(SELECT COUNT(*) FROM ' . DIVISIONS_TABLE . 
						' WHERE d_champid = ' .  $this->champid . ') WHERE c_id = ' . $this->champid;
			$result = $db->sql_query($qUpdateDivisions);
			$this->mess = new addMessage(str_replace('{entry}', $_POST['title'], $lang['AdminAdded']), $lang['AdminAddedTitle']);
		}
		else
		{
			$this->mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
		}
	}
	
	function doEdit()
	{
		global $db, $lang;
		
		if($this->validateFields(true)) 
		{
			$qUpd = "update  ". DIVISIONS_TABLE . " set d_title = '" . $_POST['title'] .
													  "', d_order = " . $_POST['order'] .
													  ", d_signuplimit = " . $_POST['signuplimit']  .
													  ", d_drivermaxevents = " . $_POST['eventslimit']  .
													  ", d_ranking = " .  $_POST['ranking'] . 
													  " WHERE d_id = " . $_POST['id'] ;
			
			$result = $db->sql_query($qUpd);
			
			$this->mess = new addMessage(str_replace('{entry}', $_POST['title'], $lang['AdminUpdated']), $lang['AdminUpdatedTitle']);
		}
		else
		{
			$this->mess = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
		}
	}
	
	function validateFields($checkID = false)
	{
		$valid = true;
		
		if($checkID)
		{
			$valid = is_numeric($_POST['id']);
			
		}
		
		
		return $valid && is_numeric($_POST['order'])
 					  && is_numeric($_POST['signuplimit'])
					  && $this->champid
 					  && ($_POST['title'] != '') 
					  && is_numeric($_POST['eventslimit'])
					  && is_numeric($_POST['ranking'])
					  && $_POST['ranking'] > 0 
					  && $_POST['ranking'] <= 20; 
					  
	}
	
	function setupEdit()
	{
		global $db, $lang;
		include_once('includes/addMessage.php');
		include_once('admin/edit_division.php');
		
		//get the record
		$q = "Select * From ". DIVISIONS_TABLE . " Where d_champid = " . $this->champid;
		$result = $db->sql_query($q);
		$introMess = false;
		while($row = $db->sql_fetchrow($result)) 
		{
			if(!$introMess)
			{
				$this->toRender[] = new addMessage($lang['AdminEdit'], $lang['AdminEditTitle']);
				$introMess = true;
			}
			
			$this->toRender[] = new edit_division(false, $this->champid, $row);
		}
	}

	function setupAdd()
	{
		global $db, $lang;
		include_once('includes/addMessage.php');
		include_once('admin/edit_division.php');
		
		
		$this->toRender[] = new addMessage($lang['AdminAdd'], $lang['AdminAddTitle']);
		$this->toRender[] = new edit_division(true, $this->champid);
	}

	
}
?>